#include "../import/head.h"
#include "helper_protocol.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "../helper_package/head.h"
#include "../helper_heart/head.h"
#include "../helper_http/head.h"
#include "../helper_net/head.h"
#include "../helper_tcp/head.h"
#include "../helper_udp/head.h"
#include "protocol_disp_connect_num.h"
#include "protocol_gate_stock_list.h"
#include "protocol_common_heart.h"
#include "protocol_client_ssl.h"

namespace xos_sgsj
{

    helper_protocol::helper_protocol()
    {
    }

    helper_protocol::~helper_protocol()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int helper_protocol::helper_proc( xos::i_buf * pBuf, session * pSession )
    {
        int ret = 0;

        xos::xos_u16 uCmd = PROTOCOL_CMD_UNKNOWN;

        if( 0 == ret )
        {
            ret = g_pSysData->m_os.m_pSerial->un_pack_int16( pBuf, &uCmd );
        }

        if( 0 == ret )
        {
            switch( uCmd )
            {
                case PROTOCOL_DISP_CONNECT_NUM:
                    {
                        protocol_disp_connect_num obj;
                        ret = obj.proc( pBuf, pSession );
                    }
                    break;
                case PROTOCOL_GATE_STOCK_LIST:
                    {
                        protocol_gate_stock_list obj;
                        ret = obj.proc( pBuf, pSession );
                    }
                    break;
                case PROTOCOL_COMMON_HEART:
                    {
                        protocol_common_heart obj;
                        ret = obj.proc( pBuf, pSession );
                    }
                    break;
                case PROTOCOL_CLIENT_SSL:
                    {
                        protocol_client_ssl obj;
                        ret = obj.proc( pBuf, pSession );
                    }
                    break;
                default:
                    {
                        ret = 1;
                    }
                    break;
            }
        }

        {
            data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
            p->init();
            p->m_pSession = pSession;
            p->m_nMsg = MSG_PROTOCOL;
            p->m_bSuccess = ( 0 == ret );
            g_pSysData->m_pImpl->notify( p );
        }

        if( pBuf )
        {
            pBuf->release();
            pBuf = 0;
        }

        return ret;
    }

    int helper_protocol::on_connect( session * pSession, bool bSuccess )
    {
        int ret = 0;

        if( ( 0 == ret ) && ( ( session::CONNECT_TYPE_TRADE != pSession->m_eConnectType ) ) )
        {
            ret = 1;
        }

        if( ( 0 == ret ) && !bSuccess )
        {
            ret = 1;
        }

        if( 0 == ret )
        {
            g_pSysData->m_pPool->m_trade_list.push_back( pSession );
        }

        if( 0 == ret )
        {
            protocol_gate_stock_list obj;
            ret = obj.post_for_stock_list( pSession );
        }

        return ret;
    }

    int helper_protocol::heart()
    {
        int ret = 0;
        protocol_common_heart obj;
        obj.heart();
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // proc

    int helper_protocol::proc( data * pData )
    {
        int ret = 0;

        session * pSession = pData->m_pSession;

        if( pData->m_bSuccess )
        {
            ret = pSession->sub_ref( 1 );
        }
        else
        {
            ret = pSession->sub_ref( 2 );
        }

        return ret;
    }

    int helper_protocol::run_task( xos::i_msg * pMsg )
    {
        int ret = 0;

        session * pSession = ( session * ) pMsg->get_void( 0, 0 );
        xos::i_buf * pBuf = pMsg->get_buf( 0, 0 );

        ret = helper_proc( pBuf, pSession );

        if( pMsg )
        {
            pMsg->release();
            pMsg = 0;
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

} // xos_sgsj
